//
//  USMomentsCell.m
//  us
//
//  Created by karry on 2025/6/2.
//

#import "USMomentsCell.h"
#import "USMomentsPhotoCell.h"
@interface USMomentsCell ()<UICollectionViewDelegate,UICollectionViewDataSource>


@property (nonatomic, strong) UIView *leftView;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *contentLab;

@property (nonatomic, strong) UICollectionView *photoCollectionView;

@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UIButton *commentBtn;


@end

@implementation USMomentsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    self.leftView = [[UIView alloc]init];
    [self.contentView addSubview:self.leftView];
    self.leftView.layer.cornerRadius = 2;
    self.leftView.layer.masksToBounds = YES;
    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(28);
        make.height.mas_equalTo(14);
        make.width.mas_equalTo(4);
    }];
    
    self.nameLab = [[UILabel alloc]init];
    [self.contentView addSubview:self.nameLab];
    self.nameLab.font = [UIFont systemFontOfSize:14];
    self.nameLab.textColor = [UIColor.titleColor jk_lightenColor:0.8];
    self.nameLab.textAlignment = NSTextAlignmentLeft;
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.leftView);
        make.left.equalTo(self.leftView.mas_right).offset(6);
    }];
    
    self.contentLab = [[UILabel alloc]init];
    [self.contentView addSubview:self.contentLab];
    self.contentLab.font = [UIFont systemFontOfSize:16];
    self.contentLab.textColor = UIColor.titleColor;
    self.contentLab.textAlignment = NSTextAlignmentLeft;
    self.contentLab.numberOfLines = 0;
    [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftView.mas_bottom).offset(12);
        make.left.mas_equalTo(24);
        make.right.mas_equalTo(-24);
    }];
    
 
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat margin = 2;
    CGFloat w = 82;
    layout.itemSize = CGSizeMake(w, w);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = margin;
    layout.minimumInteritemSpacing = margin;
    self.photoCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.photoCollectionView.dataSource = self;
    self.photoCollectionView.delegate = self;
    self.photoCollectionView.backgroundColor = UIColor.clearColor;
 
    [self.photoCollectionView registerClass:[USMomentsPhotoCell class] forCellWithReuseIdentifier:@"USMomentsPhotoCell"];
    [self.contentView addSubview:self.photoCollectionView];
    [self.photoCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLab.mas_bottom).offset(8);
        make.left.mas_equalTo(self.contentLab);
        make.width.mas_equalTo(82 * 3 + 4);
        make.height.mas_equalTo(0);
    }];
    
    
    self.timeLab = [[UILabel alloc]init];
    [self.contentView addSubview:self.timeLab];
    self.timeLab.font = [UIFont systemFontOfSize:12];
    self.timeLab.textColor = [UIColor.titleColor jk_lightenColor:0.8];
    self.timeLab.textAlignment = NSTextAlignmentLeft;
    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.photoCollectionView.mas_bottom).offset(8);
        make.left.mas_equalTo(self.contentLab);
        make.height.mas_equalTo(22);
        make.bottom.mas_equalTo(-12);
    }];
    
    self.commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.commentBtn];
    [self.commentBtn setImage:[UIImage imageNamed:@"moments_comment"] forState:0];
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.timeLab);
        make.right.mas_equalTo(self.contentLab);
        make.width.height.mas_equalTo(24);
    }];
    [self.commentBtn addTarget:self action:@selector(commentAction) forControlEvents:UIControlEventTouchUpInside];

    
    self.editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.editBtn];
    [self.editBtn setImage:[UIImage imageNamed:@"moments_edit"] forState:0];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.timeLab);
        make.right.equalTo(self.commentBtn.mas_left).offset(-12);
        make.width.height.mas_equalTo(24);
    }];
 
}
 
- (void)commentAction {
    if (self.commentBlock) {
        self.commentBlock();
    }
}

- (void)setBreakMode:(BOOL)breakMode {
    _breakMode = breakMode;
    
}
- (void)setModel:(USMomentsModel *)model {
    _model = model;
    
    NSString *dayStr = [NSDateFormatter localizedStringFromDate:[NSDate dateWithTimeIntervalSince1970:self.model.moodDiaryDto.gmtCreate] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle];

    
    if ([model.moodDiaryDto.userId isEqualToString: USUser.user.userId]) {
        self.leftView.backgroundColor = [UIColor jk_colorWithHex:0xFF967C];
        self.nameLab.text = [NSString stringWithFormat:@"%@·我的",dayStr];
        self.editBtn.hidden = NO;
    }else{
        self.leftView.backgroundColor = UIColor.us_blueColor;
        self.nameLab.text = [NSString stringWithFormat:@"%@·%@的",dayStr,USUser.user.relUser.nickname];
        self.editBtn.hidden = YES;

    }
    
    self.timeLab.text = dayStr;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.minimumLineHeight = 28;
    paragraphStyle.maximumLineHeight = 28;
    
    if (self.breakMode) {
        paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
        self.contentLab.numberOfLines = 5;
        self.contentLab.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    NSDictionary *attributes = @{
        NSParagraphStyleAttributeName: paragraphStyle,
    };

    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:model.moodDiaryDto.diaryContent?:@"" attributes:attributes];
    self.contentLab.attributedText = attributedText;
    

    CGFloat height = 0;
    if (model.photoList.count > 0) {
        if (model.photoList.count % 3 == 0) {
            height = (82 + 2) * model.photoList.count/3 - 2;
        }else{
            height = (82 + 2) * (model.photoList.count/3 + 1) - 2;
        }
    }

 

    [self.photoCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLab.mas_bottom).offset(8);
           make.left.mas_equalTo(16);
           make.width.mas_equalTo(82 * 3 + 4);
           make.height.mas_equalTo(height);
    }];
    
    [self.photoCollectionView reloadData];
}

#pragma mark - <UICollectionViewDelegate,UICollectionViewDataSource>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.photoPreviewBlock) {
        self.photoPreviewBlock(indexPath.item);
    }
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
 
}
 
 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.model.photoList.count;
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    USMomentsPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"USMomentsPhotoCell" forIndexPath:indexPath];
    
    USPhotoModel *model = self.model.photoList[indexPath.row];
    
    cell.photoModel = model;
    cell.isEdit = YES;
    return cell;
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
